package com.xxx.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.course.common.Result;
import com.xxx.course.entity.EduTeacher;
import com.xxx.course.entity.vo.TeacherQuery;
import com.xxx.course.service.EduTeacherService;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author chn
 * @since 2022-05-27
 */
@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("getAll")
    public Result getAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("teacher",list);
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        eduTeacherService.removeById(id);
        return Result.ok();
    }
   
    @PostMapping("page/condition/{current}/{limit}")
    public Result pageCondition(@PathVariable("current") long current
                                ,@PathVariable("limit") long limit
                                ,@RequestBody TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create","gmt_modified");
        eduTeacherService.page(page, wrapper);
        return Result.ok().data("page",page);
    }
    @PostMapping("/add")
    public Result add(@RequestBody EduTeacher eduTeacher){
        return eduTeacherService.save(eduTeacher)?Result.ok():Result.error();
    }
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return Result.ok().data("teacher",teacher);
    }
    @PostMapping("/update")
    public Result get(@RequestBody EduTeacher teacher){
        return eduTeacherService.updateById(teacher)?Result.ok():Result.error();
    }

}

